package com.huluobo.high3demo.setting;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.databinding.ActivitySettingBinding;
import com.huluobo.high3demo.login.LoginActivity;
import com.huluobo.mvvm.baen.SettingBean;
import com.huluobo.mvvm.utils.ImageLoader;
import com.huluobo.mvvm.view.BaseMVVMActivity;
import com.hyphenate.chat.EMClient;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import org.w3c.dom.Text;

public class SettingActivity extends BaseMVVMActivity<ActivitySettingBinding, SettingViewModel> {
    @Override
    protected void initData() {
        viewModel.updateIconSuccess.observe(this, settingBean -> {
            ToastUtils.showLong("修改头像成功");
            ImageLoader.getInstance().loadCircleImage(SettingActivity.this, settingBean.getData(),
                    dataBinding.settingUserIcon);
        });

        viewModel.updateNicknameSuccess.observe(this, settingBean -> {
            ToastUtils.showLong("修改昵称成功");
            dataBinding.settingEtNickName.setText(settingBean.getData());
        });

        viewModel.updateAddressSuccess.observe(this, settingBean -> {
            ToastUtils.showLong("修改地址成功");
            dataBinding.settingEtAddress.setText(settingBean.getData());
        });

        viewModel.updateBrithSuccess.observe(this, settingBean -> {
            ToastUtils.showLong("修改生日成功");
            dataBinding.settingEtBrith.setText(settingBean.getData());
        });

        viewModel.updateSexSuccess.observe(this, settingBean -> {
            ToastUtils.showLong("修改性别成功");
            dataBinding.settingEtSex.setText(settingBean.getData());
        });
    }

    @Override
    protected void initView() {
        dataBinding.settingUserIcon.setOnClickListener(v -> selectLocPic());
        dataBinding.settingBtnAddress.setOnClickListener(v -> updateUserAddress());
        dataBinding.settingBtnNickName.setOnClickListener(v -> updateUserNickName());
        dataBinding.settingBtnBrith.setOnClickListener(v -> updateUserBrith());
        dataBinding.settingBtnSex.setOnClickListener(v -> updateUserSex());
        dataBinding.settingBtnLogout.setOnClickListener(v -> logout());
    }

    //退出登录
    private void logout() {
        //清空SP中所有信息
        SPUtils.getInstance().clear();
        //退出IM
        EMClient.getInstance().logout(true);
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    //更新用户性别
    private void updateUserSex() {
        String sex = dataBinding.settingEtSex.getText().toString();
        if (!TextUtils.isEmpty(sex)) {
            viewModel.updateUserSex(sex);
        }
    }

    //更新用户生日
    private void updateUserBrith() {
        String brith = dataBinding.settingEtBrith.getText().toString();
        if (!TextUtils.isEmpty(brith)) {
            viewModel.updateUserBrith(brith);
        }
    }

    //更新用户昵称
    private void updateUserNickName() {
        String nickName = dataBinding.settingEtNickName.getText().toString();
        if (!TextUtils.isEmpty(nickName)) {
            viewModel.updateUserNickname(nickName);
        }
    }

    //更新用户地址
    private void updateUserAddress() {
        String address = dataBinding.settingEtAddress.getText().toString();
        if (!TextUtils.isEmpty(address)) {
            viewModel.updateUserAddress(address);
        }
    }

    //更新用户头像
    private void selectLocPic() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }

    //图片选择器的回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            PictureBean bean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            String filePath = bean.getPath();
            if (!TextUtils.isEmpty(filePath)) {
                ToastUtils.showLong("上传头像中...");
                viewModel.updateUserIcon(filePath);
            }
        }
    }

    @Override
    protected Class<SettingViewModel> getViewModel() {
        return SettingViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }
}